package com.it.exprezoorecords.json.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import utility.ApplicationProperties;
import utility.Networking;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.it.exprezoorecords.R;
import com.it.exprezoorecords.R.string;
import com.it.exprezoorecords.json.JSONfunctions;

	public class SongsListActivity extends ListActivity {
	    private static final String TAG = "Main";
	    
	    Intent intentSong; 
	    JSONArray  songsList = null;

		/** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	    	
	        super.onCreate(savedInstanceState);	        
	       
	        if(Networking.isNetworkAvailable(getApplicationContext()))
	        {
	        	new BackgroundWorker().execute(null, null, null);	
	        	setContentView(R.layout.listplaceholder); 
	        }else{
	        	setContentView(R.layout.listplaceholder); 
	        	Toast.makeText(SongsListActivity.this, string.noconnection, Toast.LENGTH_LONG).show();
	        }
	               
	        final ListView lv = getListView();
	        lv.setTextFilterEnabled(true);	
	        lv.setOnItemClickListener(new OnItemClickListener() {
	        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        		
	    			intentSong = new Intent(getApplicationContext(), SongActivity.class);	    			
	    			try {
						JSONObject currentObj = songsList.getJSONObject(position);
						intentSong.putExtra("title", currentObj.getString("title"));
						intentSong.putExtra("author", currentObj.getString("author"));
						intentSong.putExtra("track", currentObj.getString("track"));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	        		startActivity(intentSong);
				}
			});
	  
	    }
	
	class BackgroundWorker extends AsyncTask<Void, Void, Void>{
		ArrayList<HashMap<String, String>> mylist;
		 private ProgressDialog myProgressDialog;
		 
		protected void onPreExecute() {
	    	//myProgressDialog = new ProgressDialog(Main.this); 
			myProgressDialog=ProgressDialog.show(SongsListActivity.this, "", "Your Exprezoo Music...", true);
		} 
		 
		@Override
		protected Void doInBackground(Void... params) {
	    	
			// TODO Auto-generated method stub
	        mylist = new ArrayList<HashMap<String, String>>();	        
	        JSONObject json = JSONfunctions.getJSONfromURL(ApplicationProperties.TRACCE_JSON_URL);
	        try{
	        	songsList = json.getJSONArray("song");
	        	Log.d(TAG, "LEN......................: " + songsList.length());
		        for(int i=0;i<songsList.length();i++){						
					HashMap<String, String> map = new HashMap<String, String>();	
					JSONObject e = songsList.getJSONObject(i);
					
					map.put("id",  String.valueOf(i));
		        	map.put("track", "track:" + e.getString("track"));
		        	map.put("title", "Title: " +  e.getString("title"));
		        	map.put("author", "Author:" + e.getString("author"));
		        	Log.d("Main-----------------------------", e.getString("track") + e.getString("title") + e.getString("author"));
		        	mylist.add(map);			
				}		
	        }catch(JSONException e) {
	        	 Log.e("log_tag", "Error parsing data "+e.toString());
	        }
	        	        

	    	
			return null;
		}
		
		protected void onPostExecute(Void result)
		{
			myProgressDialog.dismiss();
	        ListAdapter adapter = new SimpleAdapter(SongsListActivity.this, mylist, R.layout.mainjson, new String[] { "title", "author", "track" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });    
	        setListAdapter(adapter);
	        
		}

		
	}
	
	
	
	
	}

